package masterjava.common.client.gxt.base;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.*;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import masterjava.common.client.gwt.settings.AppSettings;
import masterjava.common.client.gxt.base.info.InfoDialog;
import masterjava.common.shared.settings.AppSettingKeys;

public class RootLayout extends Viewport {

    private ContentPanel centerPanel;
    private HtmlContainer headingPanel;
    private ContentPanel navigationPanel;
    private InfoDialog aboutDialog = null;

    private static RootLayout INSTANCE = new RootLayout();

    private RootLayout() {
        setLayout(new BorderLayout());
        addNorth();
        addCenter();
    }

    public static RootLayout get() {
        return INSTANCE;
    }

    public void setAppTitle(String title) {
        headingPanel.add(new Text(title), "#head-title");
    }

    public void setAppDetails() {
        String title = AppSettings.<String>get(AppSettingKeys.appName);
        setAppTitle(title);
        aboutDialog = InfoDialog.getInfoDialog("About " + title, AppSettingKeys.applicationHelp);
        aboutDialog.setWidth(400);
    }

    protected void addNorth() {
        headingPanel = createHeading();
        BorderLayoutData data = new BorderLayoutData(LayoutRegion.NORTH, 30);
        data.setMargins(new Margins());
        add(headingPanel, data);
    }

    protected void addCenter() {
        centerPanel = new ContentPanel();
        centerPanel.setBorders(false);
        centerPanel.setHeaderVisible(false);
        centerPanel.setLayout(new FitLayout());
        BorderLayoutData data = new BorderLayoutData(LayoutRegion.CENTER);
        data.setMargins(new Margins(5, 5, 5, 0));
        add(centerPanel, data);
    }

    public void setNavigation(ContentPanel panel) {
        navigationPanel = panel;
        BorderLayoutData data = new BorderLayoutData(LayoutRegion.WEST, 220, 150, 350);
        data.setMargins(new Margins(5, 0, 5, 5));
        data.setCollapsible(true);
//        data.setSplit(true);
        navigationPanel.setBodyBorder(false);
//        navigationPanel.setLayout(new AccordionLayout());
//        navigationPanel.setLayoutOnChange(true);
        add(navigationPanel, data);
        navigationPanel.layout();
    }

    public void setContent(Component newContent) {
        centerPanel.removeAll();
        centerPanel.add(newContent);
        centerPanel.layout();
    }

    protected HtmlContainer createHeading() {
        HtmlContainer northPanel = new HtmlContainer("<div id=head-help></div><div id=head-title></div>");
        northPanel.setStateful(false);
        Button help = new Button("About", new SelectionListener<ButtonEvent>() {
            public void componentSelected(ButtonEvent ce) {
                if (aboutDialog != null) {
                    aboutDialog.show();
                }
            }
        });
        northPanel.add(help, "#head-help");
        return northPanel;
    }

//    abstract protected Component getInfoComponent();
}

